package Package4;

import java.util.Random;
import java.util.Scanner;

public class stage1 {
    public static void main(String[] args) {
        test1();//买飞机票
         test2();//找101 ~ 200 之间所以素数
        test3();//开发验证码
         test4();//评委打分
        test5();//数字加密
        test6();//解密数字
        test7();//抽奖
        test_7();//抽奖代码优化

    }

    public static void test1() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入飞机票原价：");
        int price = sc.nextInt();
        System.out.println("请输入月份：");
        int month = sc.nextInt();
        System.out.println("请输入头等舱还是经济舱(0头等舱， 1 经济舱)：");
        int seat = sc.nextInt();
        //旺季
        if (month >= 5 && month <= 10) {
            price = getPrice(price, seat, 0.9, 0.85);
        } else if ((month >= 11 && month <= 12) || (month >= 1 && month <= 4)) {//淡季
            price = getPrice(price, seat, 0.7, 0.65);
        } else {
            System.out.println("键盘录入的月分不合法！");
        }
        System.out.println("飞机票的价格为：" + price);
    }

    //抽象方法
    public static int getPrice(int price, int seat, double v0, double v1) {
        if (seat == 0) {
            price *= v0;
        } else if (seat == 1) {
            price *= v1;
        } else {
            System.out.println("没有这个仓位！");
        }
        return price;
    }

    public static void test2() {
        int count = 0;
        for (int i = 101; i <= 200; i++) {
            boolean num = judgeNum(i);
            if (num) {
                System.out.println(i);
                count++;
            }
        }
        System.out.println("一共有：" + count + "个素数");
    }

    public static boolean judgeNum(int i) {
        for (int j = 2; j < i; j++) {
            if (i % j == 0) {
                return false;
            }
        }
        return true;
    }

    public static void test3() {
        char[] chas = new char[52];
        //将26个字母大小写保存在数组中
        for (int i = 0; i < chas.length; i++) {
            if (i <= 25) {
                chas[i] = (char) ('A' + i);
            } else {
                chas[i] = (char) ('a' + i - 26);
            }
        }
        Random r = new Random();
        //定义最终结果字符串
        String result = "";
        for (int i = 0; i < 4; i++) {
            int randomIndex = r.nextInt(chas.length);
            result += chas[randomIndex];
        }
        int num = r.nextInt(10);
        result += num;
        System.out.println("验证码是：" + result);
    }

    public static void test4() {
        int[] arr = getArr();
        //去掉最大值和最小值以及和
        int max = arr[0];
        int min = arr[0];
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
            if (arr[i] < min) {
                min = arr[i];
            }
            sum += arr[i];
        }
        //求平均值
        int avg = (sum - max - min) / (arr.length - 2);
        System.out.println("平均分是： " + avg);

    }

    public static int[] getArr() {
        int[] arr = new int[6];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入评分：");
            int score = sc.nextInt();
            if (score >= 0 && score <= 100) {
                arr[i] = score;
            } else {
                System.out.println("选手评分不合理");
                --i;
            }
        }
        return arr;
    }

    public static void test5() {
        Scanner sc = new Scanner(System.in);
        System.out.println("你要加密的数字是：");
        int number = sc.nextInt();
        int x = number;
        int count = 0;
        while (x != 0) {
            x /= 10;
            count++;
        }
        int[] arr = new int[count];
        for (int i = arr.length - 1; i >= 0; i--) {
            arr[i] = number % 10;
            number /= 10;
        }
        //加密数字
        for (int i = 0; i < arr.length; i++) {
            arr[i] += 5;
            arr[i] %= 10;
        }
        //将数字反转
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        //取数字
        int newnumber = 0;
        for (int i = 0; i < arr.length; i++) {
            newnumber = newnumber * 10 + arr[i];
        }
        System.out.println("加密后的数字是：" + newnumber);
    }

    public static void test6() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要解密的数字：");
        int number = sc.nextInt();
        int tempNum = number;
        int count = 0;
        while (number != 0) {
            number /= 10;
            count++;
        }
        int[] arr = new int[count];
        //存入数组
        for (int i = arr.length - 1; i >= 0; i--) {
            arr[i] = tempNum % 10;
            tempNum /= 10;
        }

        //反转
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        //解密
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 0 && arr[i] <= 4) {
                arr[i] += 10;
            }
            arr[i] -= 5;
        }

        //链接
        int num = 0;
        for (int i = 0; i < arr.length; i++) {
            num = num * 10 + arr[i];
        }
        System.out.println("解密后为：" + num);
    }

    public static void test7() {
        int[] arr = {2, 588, 1000, 800, 5000};
        int[] newArr = new int[arr.length];

        Random r = new Random();
        for (int i = 0; i < arr.length; ) {
            int randomIndex = r.nextInt(arr.length);
            int prize = arr[randomIndex];
            if (!contains(newArr, prize)) {
                newArr[i] = prize;
                i++;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(newArr[i]);
        }
    }

    public static boolean contains(int[] arr, int prize) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == prize) {
                return true;
            }
        }
        return false;
    }

    //代码优化
    public static void test_7() {
        int[] arr = {2, 588, 1000, 800, 5000};
        Random r = new Random();
        //打乱数组
        for (int i = 0; i < arr.length; i++) {
            int randomIndex = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "  ");
        }
    }
}
